<template>
  <div class="case">
    <na-table
      :datas.sync="tableData"
      :searchObj.sync="search"
      @updateDatas="getAllPurchase"
      @newAdd="addPurchaseDialog"
    >
    </na-table>
    <!-- 人员寻找弹框   -->
    <per-redio-dialog
      :per-visible.sync="isperShow"
      @getEmployee="getEmployee"
    />
    <!-- 新建采购   -->
    <el-dialog
      :title="ops === 'modify' ? '采购编辑' : '新建采购'"
      :visible.sync="isAddPurchase"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="purchaseForm"
            :model="purchaseForm"
            :rules="rules"
            label-width="110px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="采购单号" prop="purchaseOrderNo">
                  <el-input
                    v-model="purchaseForm.purchaseOrderNo"
                    size="small"
                    disabled
                    placeholder="系统自动生成"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="purchaseOrderNo">
                  <el-input
                    :value="purchaseForm.createTime || getDate()"
                    size="small"
                    placeholder="系统自动生成"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="创建人员"
                  prop="purchaseOrderNo"
                  v-if="ops === 'modify'"
                >
                  <el-input
                    v-model="purchaseForm.createPersonName"
                    size="small"
                    placeholder="创建人员"
                    disabled
                  />
                </el-form-item>
                <el-form-item label="创建人员" prop="purchaseOrderNo" v-else>
                  <el-input :value="name" disabled size="small" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="8">
                <el-form-item label="采购类型" prop="purchaseType">
                  <el-select
                        filterable
                    v-model="purchaseForm.purchaseType"
                    placeholder="采购类型"
                    size="mini"
                  >
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierCode">
                  <el-select
                        filterable
                    v-model="purchaseForm.supplierCode"
                    placeholder="供应商"
                    size="mini"
                    filterable
                    @change="supplierChange"
                  >
                    <el-option
                      v-for="item in allSupliers"
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="期望到货日期" prop="expectArrivalTime">
                  <el-date-picker
                    v-model="purchaseForm.expectArrivalTime"
                    type="date"
                    :picker-options="option"
                    placeholder="选择日期"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="采购备注" class="inlineText">
              <el-input
                v-model="purchaseForm.purchaseDesc"
                type="textarea"
                :rows="1"
                placeholder="采购备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">采购明细</span>
            </div>
            <div ref="bigdialogTable" class="bigdialogTbCon">
              <el-table
                :data="purchaseForm.purchaseDetailsList"
                :row-key="(row) => row.rowKey"
                border
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  prop="specificationCode"
                  width="120"
                  align="center"
                  label="规格编号"
                >
                </el-table-column>
                <el-table-column
                  prop="materialType"
                  width="90"
                  align="center"
                  label="物料类型"
                >
                  <template slot-scope="scope">
                    {{ scope.row.goodsType }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="school"
                  width="200"
                  align="center"
                  label="物料名称"
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.goodsCode"
                      placeholder="请选择物料"
                      size="small"
                      @change="
                        handleMaterialNameChange(
                          scope.row,
                          scope.$index,
                          $event
                        )
                      "
                    >
                      <el-option
                        :label="item.goodsName"
                        :value="item.goodsCode"
                        v-for="(item, index) in materialList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="specificationCode"
                  width="120"
                  align="center"
                  label="规格型号"
                  filterable
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.specificationCode"
                      @change="
                        handleSpecificationChange(scope.row, scope.$index)
                      "
                      placeholder="请选择规格型号"
                      size="small"
                    >
                      <el-option
                        :label="item.specificationName"
                        :value="item.specificationCode"
                        v-for="(item, index) in scope.row
                          .goodsSpecificationList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="stockQuantity"
                  width="120"
                  align="center"
                  label="库存数量"
                >
                  <template slot-scope="{ row }">
                    {{ row.stockQuantity }}
                  </template>
                </el-table-column>
<!--                <el-table-column
                  prop="purchaseQuantity"
                  width="150"
                  align="center"
                  label="入库数量"
                >
                </el-table-column>-->
                <el-table-column
                  prop="planPurchaseQuantity"
                  width="120"
                  align="center"
                >
                  <template slot="header" slot-scope="scope">
                    <el-tooltip placement="top">
                      <div slot="content">
                        <div>
                          采购数量（使用单位） * 转换比例 = 计价单位的数量<br />计价单位的数量
                          * 单价 = 小计
                        </div>
                      </div>
                      <span
                        >采购数量<i
                          class="el-icon-info pl8"
                          style="color: #666"
                        ></i
                      ></span>
                    </el-tooltip>
                  </template>
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.planPurchaseQuantity"
                      placeholder="请输入"
                      size="mini"
                      onkeyup="value=value.replace(/[^\d]/g,'')"
                      @blur="getPurTotalPrice(row)"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="goodsUnit"
                  width="100"
                  align="center"
                  label="使用单位"
                >
                </el-table-column>
                <el-table-column
                  prop="specificationJsValue"
                  width="100"
                  align="center"
                  label="转换比例"
                >
                </el-table-column>
                <el-table-column
                  prop="priceUnit"
                  width="100"
                  align="center"
                  label="计价单位"
                >
                </el-table-column>
                <el-table-column
                  prop="goodsPrice"
                  width="100"
                  align="center"
                  label="单价"
                >
                </el-table-column>
                <el-table-column
                  prop="subtotal"
                  width="100"
                  align="center"
                  label="小计"
                >
                  <template slot-scope="{ row }">
                    {{ row.subtotal }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="materialRemark"
                  align="center"
                  width="220"
                  label="备注"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.purchaseDesc"
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作"
                  width="100px"
                  fixed="right"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-button
                      v-if="
                        purchaseForm.purchaseDetailsList.length - 1 !=
                        scope.$index
                      "
                      type="text"
                      @click="handleCopyRow(scope.row, scope.$index)"
                      >复制</el-button
                    >
                    <el-button
                      type="text"
                      v-if="
                        purchaseForm.purchaseDetailsList.length - 1 !=
                        scope.$index
                      "
                      @click="handleDeleteRow(scope.row, scope.$index)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddPurchase = false">取 消</el-button>
        <!-- 新增       -->
        <el-button
          v-if="ops === 'add'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="savePuchase"
          >保存</el-button
        >
        <el-button
          v-if="ops === 'add'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="savePuchase('confirm')"
          >保存并完成</el-button
        >
        <!-- 复制重开       -->
        <!-- <el-button
          v-if="ops === 'copy'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="copyReopen"
          >保存</el-button
        > -->
        <!--  修改      -->
        <el-button
          v-if="ops === 'modify'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="editSavePuchase"
          >保存</el-button
        >
        <el-button
          v-if="ops === 'modify'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="editSavePuchase('confirm')"
          >保存并完成</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看采购--没到货之前的查看弹框   -->
    <el-dialog
      title="查看采购"
      :visible.sync="isSeePurchase"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="purchaseForm"
            :model="purchaseForm"
            :rules="rules"
            label-width="110px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="采购单号" prop="purchaseOrderNo">
                  <el-input
                    v-model="purchaseForm.purchaseOrderNo"
                    size="small"
                    disabled
                    placeholder="系统自动生成"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="purchaseOrderNo">
                  <el-input
                    :value="purchaseForm.createTime"
                    size="small"
                    placeholder="系统自动生成"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="创建人员"
                  prop="purchaseOrderNo"
                  v-if="ops === 'modify'"
                >
                  <el-input
                    v-model="purchaseForm.createPersonName"
                    size="small"
                    placeholder="创建人员"
                    disabled
                  />
                </el-form-item>
                <el-form-item label="创建人员" prop="purchaseOrderNo" v-else>
                  <el-input :value="name" disabled size="small" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="8">
                <el-form-item label="采购类型" prop="purchaseType">
                  <el-select
                        filterable
                    v-model="purchaseForm.purchaseType"
                    disabled
                    placeholder="采购类型"
                    size="mini"
                  >
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierCode">
                  <el-select
                        filterable
                    v-model="purchaseForm.supplierCode"
                    placeholder="供应商"
                    disabled
                    size="mini"
                    filterable
                    @change="supplierChange"
                  >
                    <el-option
                      v-for="item in allSupliers"
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="期望到货日期" prop="expectArrivalTime">
                  <el-date-picker
                    v-model="purchaseForm.expectArrivalTime"
                    type="date"
                    :picker-options="option"
                    disabled
                    placeholder="选择日期"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="采购备注" class="inlineText">
              <el-input
                v-model="purchaseForm.purchaseDesc"
                type="textarea"
                :rows="2"
                disabled
                placeholder="采购备注"
                size="mini"
                clearable
              />
            </el-form-item>
            <el-form-item
              label="拒绝原因"
              class="inlineText mt8"
              v-if="purchaseForm.cancelReason"
            >
              <el-input
                v-model="purchaseForm.cancelReason"
                type="textarea"
                :rows="2"
                placeholder="拒绝原因"
                size="mini"
                clearable
                disabled
              />
            </el-form-item>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">采购明细</span>
            </div>
            <div class="bigdialogTbCon">
              <el-table
                :data="purchaseForm.purchaseDetailsList"
                :row-key="(row) => row.rowKey"
                border
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  prop="specificationCode"
                  width="150"
                  align="center"
                  label="规格编号"
                >
                </el-table-column>
                <el-table-column
                  prop="materialType"
                  width="150"
                  align="center"
                  label="物料类型"
                >
                  <template slot-scope="scope">
                    {{ scope.row.goodsType }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="school"
                  width="150"
                  align="center"
                  label="物料名称"
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.goodsCode"
                      placeholder="请选择物料"
                      disabled
                      size="small"
                      @change="
                        handleMaterialNameChange(
                          scope.row,
                          scope.$index,
                          $event
                        )
                      "
                    >
                      <el-option
                        :label="item.goodsName"
                        :value="item.goodsCode"
                        v-for="(item, index) in materialList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="specificationCode"
                  width="150"
                  align="center"
                  label="规格型号"
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.specificationCode"
                      @change="
                        handleSpecificationChange(scope.row, scope.$index)
                      "
                      placeholder="请选择规格型号"
                      disabled
                      size="small"
                    >
                      <el-option
                        :label="item.specificationName"
                        :value="item.specificationCode"
                        v-for="(item, index) in scope.row
                          .goodsSpecificationList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="stockQuantity"
                  width="150"
                  align="center"
                  label="库存数量"
                >
                  <template slot-scope="{ row }">
                    {{ row.stockQuantity }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="purchaseQuantity"
                  width="150"
                  align="center"
                  label="入库数量"
                >
                </el-table-column>
                <el-table-column
                  prop="planPurchaseQuantity"
                  width="150"
                  align="center"
                  label="采购数量"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.planPurchaseQuantity"
                      placeholder="请输入"
                      size="mini"
                      disabled
                      onkeyup="value=value.replace(/[^\d]/g,'')"
                      @blur="getPurTotalPrice(row)"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  prop="goodsUnit"
                  width="150"
                  align="center"
                  label="使用单位"
                >
                </el-table-column>
                <el-table-column
                  prop="specificationJsValue"
                  width="150"
                  align="center"
                  label="转换比例"
                >
                </el-table-column>
                <el-table-column
                  prop="priceUnit"
                  width="150"
                  align="center"
                  label="计价单位"
                >
                </el-table-column>
                <el-table-column
                  prop="goodsPrice"
                  width="100"
                  align="center"
                  label="单价"
                >
                </el-table-column>
                <el-table-column
                  prop="subtotal"
                  width="150"
                  align="center"
                  label="小计"
                >
                  <template slot-scope="{ row }">
                    {{ row.subtotal }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="materialRemark"
                  align="center"
                  label="备注"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.purchaseDesc"
                      disabled
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSeePurchase = false">取 消</el-button>
        <!-- <el-button
          v-if="!isSee"
          type="primary"
          size="mini"
          @click="isSeePurchase = false"
          >确定</el-button
        > -->
        <el-button
          v-if="ops == 'confirm'"
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="confirmPuchase"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <!-- 到货确认弹框   -->
    <el-dialog
      title="采购申请-审核"
      :visible.sync="isArrivalConfrim"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="purchaseForm"
            :model="purchaseForm"
            :rules="rules"
            label-width="110px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="采购单号" prop="purchaseOrderNo">
                  <el-input
                    v-model="purchaseForm.purchaseOrderNo"
                    size="small"
                    disabled
                    placeholder="系统自动生成"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="purchaseOrderNo">
                  <el-input
                    :value="purchaseForm.createDate || getDate()"
                    size="small"
                    placeholder="系统自动生成"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="创建人员"
                  prop="purchaseOrderNo"
                  v-if="ops === 'modify'"
                >
                  <el-input
                    v-model="purchaseForm.createPersonName"
                    size="small"
                    placeholder="创建人员"
                    disabled
                  />
                </el-form-item>
                <el-form-item label="创建人员" prop="purchaseOrderNo" v-else>
                  <el-input :value="name" disabled size="small" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="8">
                <el-form-item label="采购类型" prop="purchaseType">
                  <el-select
                        filterable
                    v-model="purchaseForm.purchaseType"
                    disabled
                    placeholder="采购类型"
                    size="mini"
                  >
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierCode">
                  <el-select
                        filterable
                    v-model="purchaseForm.supplierCode"
                    placeholder="供应商"
                    disabled
                    size="mini"
                    filterable
                    @change="supplierChange"
                  >
                    <el-option
                      v-for="item in allSupliers"
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="期望到货日期" prop="expectArrivalTime">
                  <el-date-picker
                    v-model="purchaseForm.expectArrivalTime"
                    type="date"
                    :picker-options="option"
                    disabled
                    placeholder="选择日期"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="采购备注" class="inlineText">
              <el-input
                v-model="purchaseForm.purchaseDesc"
                type="textarea"
                :rows="1"
                disabled
                placeholder="采购备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">采购明细</span>
            </div>
            <div class="bigdialogTbCon">
              <el-table
                :data="purchaseForm.purchaseDetailsList"
                :row-key="(row) => row.rowKey"
                border
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  prop="specificationCode"
                  width="150"
                  align="center"
                  label="规格编号"
                >
                </el-table-column>
                <el-table-column
                  prop="materialType"
                  width="150"
                  align="center"
                  label="物料类型"
                >
                  <template slot-scope="scope">
                    {{ scope.row.goodsType }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="school"
                  width="150"
                  align="center"
                  label="物料名称"
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.goodsCode"
                      placeholder="请选择物料"
                      disabled
                      size="small"
                      @change="
                        handleMaterialNameChange(
                          scope.row,
                          scope.$index,
                          $event
                        )
                      "
                    >
                      <el-option
                        :label="item.goodsName"
                        :value="item.goodsCode"
                        v-for="(item, index) in materialList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="specificationCode"
                  width="150"
                  align="center"
                  label="规格型号"
                >
                  <template slot-scope="scope">
                    <el-select
                        filterable
                      v-model="scope.row.specificationCode"
                      @change="
                        handleSpecificationChange(scope.row, scope.$index)
                      "
                      placeholder="请选择规格型号"
                      disabled
                      size="small"
                    >
                      <el-option
                        :label="item.specificationName"
                        :value="item.specificationCode"
                        v-for="(item, index) in scope.row
                          .goodsSpecificationList"
                        :key="index"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="stockQuantity"
                  width="150"
                  align="center"
                  label="库存数量"
                >
                  <template slot-scope="{ row }">
                    {{ row.stockQuantity }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="purchaseQuantity"
                  width="150"
                  align="center"
                  label="入库数量"
                >
                </el-table-column>
                <el-table-column
                  prop="planPurchaseQuantity"
                  width="150"
                  align="center"
                  label="采购数量"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.planPurchaseQuantity"
                      placeholder="请输入"
                      size="mini"
                      disabled
                      onkeyup="value=value.replace(/[^\d]/g,'')"
                      @blur="getPurTotalPrice(row)"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  prop="goodsUnit"
                  width="150"
                  align="center"
                  label="使用单位"
                >
                </el-table-column>
                <el-table-column
                  prop="specificationJsValue"
                  width="150"
                  align="center"
                  label="转换比例"
                >
                </el-table-column>
                <el-table-column
                  prop="priceUnit"
                  width="150"
                  align="center"
                  label="计价单位"
                >
                </el-table-column>
                <el-table-column
                  prop="goodsPrice"
                  width="100"
                  align="center"
                  label="单价"
                >
                </el-table-column>
                <el-table-column
                  prop="subtotal"
                  width="150"
                  align="center"
                  label="小计"
                >
                  <template slot-scope="{ row }">
                    {{ row.subtotal }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="materialRemark"
                  align="center"
                  label="备注"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.purchaseDesc"
                      disabled
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isArrivalConfrim = false"
          >取 消</el-button
        >
        <el-button
          v-loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="success"
          size="mini"
          @click="arrivalConfirm"
          >通过</el-button
        >
        <el-button
          v-loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="warning"
          size="mini"
          @click="handleRefuse"
          >拒绝</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看采购--到货查看弹框   -->
    <el-dialog
      title="查看采购"
      :visible.sync="isArrivalSee"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"
            ><span class="mr5">采购单号：</span
            >{{ purchaseForm.purchaseOrderNo }}</span
          >
          <span class="mr15"
            ><span class="mr5">创建日期：</span
            >{{ purchaseForm.createTime || dayjs().format("YYYY-MM-DD") }}</span
          >
          <span
            ><span class="mr5">创建人员：</span
            >{{ purchaseForm.createPersonName }}</span
          >
        </div>
        <div class="addWuliaoBox">
          <el-form
            disabled
            :model="purchaseForm"
            label-width="110px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="采购类型" prop="purchaseType">
                  <el-select
                        filterable
                    v-model="purchaseForm.purchaseType"
                    placeholder="采购类型"
                    size="mini"
                  >
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierCode">
                  <el-select
                        filterable
                    v-model="purchaseForm.supplierCode"
                    placeholder="供应商"
                    size="mini"
                    @change="supplierChange"
                  >
                    <el-option
                      v-for="item in allSupliers"
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="申请人员">
                  <el-input
                    v-model="purchaseForm.purchasePersonName"
                    placeholder="请输入内容"
                    size="mini"
                  >
                    <template slot="append">
                      <el-button
                        slot="append"
                        icon="el-icon-user"
                        @click="isperShow = true"
                      />
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="期望到货日期" prop="expectArrivalTime">
                  <el-date-picker
                    v-model="purchaseForm.expectArrivalTime"
                    type="date"
                    :picker-options="option"
                    placeholder="选择日期"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="实际到货日期" prop="expectArrivalTime">
                  <el-date-picker
                    v-model="purchaseForm.arrivalTime"
                    type="date"
                    :picker-options="option"
                    placeholder="选择日期"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="发票号码">
                  <el-input
                    v-model="purchaseForm.invoiceNumber"
                    placeholder="请输入内容"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="采购备注" class="inlineText">
              <el-input
                v-model="purchaseForm.purchaseDesc"
                type="textarea"
                :rows="3"
                placeholder="采购备注"
                size="mini"
                clearable
              />
            </el-form-item>
            <el-form-item
              label="拒绝原因"
              class="inlineText"
              v-if="purchaseForm.cancelReason"
            >
              <el-input
                v-model="purchaseForm.stockStateDesc"
                type="textarea"
                :rows="2"
                placeholder="拒绝原因"
                size="mini"
                clearable
                disabled
              />
            </el-form-item>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">采购明细</span>
            </div>
            <div class="bigdialogTbCon">
              <el-table
                :data="purchaseForm.purchaseDetailsList"
                border
                style="width: 100%"
              >
                <el-table-column
                  label="规格编号"
                  prop="specificationCode"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="物料类型"
                  prop="categoryName"
                  min-width="90"
                />
                <el-table-column
                  label="供应商"
                  prop="supplierName"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="物料名称"
                  prop="goodsName"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="规格型号"
                  prop="stockGoodsName"
                  min-width="90"
                />
                <el-table-column
                  label="采购数量"
                  show-overflow-tooltip
                  min-width="90"
                  prop="planPurchaseQuantity"
                />
                <el-table-column
                  label="到货数量"
                  prop="purchaseQuantity"
                  min-width="110"
                />
                <el-table-column
                  label="生产日期"
                  prop="dateOfManufacture"
                  min-width="200"
                >
                </el-table-column>
                <el-table-column
                  label="物料单位"
                  show-overflow-tooltip
                  min-width="90"
                  prop="goodsUnitName"
                />
                <el-table-column
                  label="上次单价/米"
                  show-overflow-tooltip
                  min-width="110"
                  prop="previousPrice"
                />
                <el-table-column
                  label="单价/米"
                  prop="purchasePrice"
                  min-width="110"
                />
                <el-table-column
                  label="换算数量"
                  show-overflow-tooltip
                  min-width="90"
                  prop="specificationQuantity"
                />
                <el-table-column
                  label="换算单位"
                  show-overflow-tooltip
                  min-width="90"
                  prop="priceUnitName"
                />
                <el-table-column
                  label="换算单价"
                  prop="specificationPrice"
                  min-width="110"
                />
                <el-table-column
                  label="总金额"
                  prop="totalAmount"
                  min-width="110"
                />
                <el-table-column
                  label="备注"
                  show-overflow-tooltip
                  prop="purchaseDesc"
                  min-width="150"
                />
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isArrivalSee = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="isArrivalSee = false"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <!-- 选择物料   -->
    <el-dialog
      title="选择物料"
      :visible.sync="isPurchaseInfo"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="70%"
    >
      <div class="dialogDody">
        <!-- 搜索     -->
        <div class="btnCon flex flex_end" style="border-bottom: none">
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="materSearch.likeWords"
              placeholder="输入关键字"
              size="small"
            >
              <el-button
                slot="append"
                v-loading="marLoading"
                type="primary"
                icon="el-icon-search"
                :disabled="marLoading"
                element-loading-spinner="el-icon-loading"
                @click="getAllMarList"
              >
                搜索
              </el-button>
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon">
          <el-table
            :data="allMateriels"
            border
            height="490"
            style="width: 100%"
            @selection-change="on_SelectSion"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column
              prop="specificationCode"
              label="规格编号"
              min-width="110"
            />
            <el-table-column
              prop="stockGoodsCode"
              label="规格编号"
              min-width="110"
            />
            <el-table-column
              prop="goodsName"
              label="物料名称"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              prop="stockGoodsName"
              label="规格型号"
              min-width="100"
            />
            <el-table-column
              prop="supplierName"
              label="供应商"
              min-width="90"
            />
            <el-table-column
              prop="categoryName"
              label="物料类型"
              min-width="90"
            />
            <el-table-column
              prop="stockSumQuantity"
              label="库存数量"
              min-width="70"
            />
            <el-table-column prop="goodsUnitName" label="单位" min-width="70" />
            <el-table-column prop="groupName" label="所属分组" min-width="90" />
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isPurchaseInfo = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="confirmSelecd"
          >确定</el-button
        >
      </span>
    </el-dialog>

    <!-- 拒绝原因   -->
    <el-dialog
      :title="title + '原因'"
      :visible.sync="reasonVisible"
      :close-on-click-modal="false"
      custom-class="comDialog"
      append-to-body
      width="30%"
      @close="closeCancelDialog"
    >
      <div class="dialogDody">
        <el-form
          ref="cancelForm"
          :model="cancelForm"
          :rules="cancelRules"
          label-width="0px"
          class="dialogForm"
        >
          <el-form-item label="" prop="reason">
            <el-input
              v-model="cancelForm.cancelReason"
              type="textarea"
              :autosize="{ minRows: 3 }"
              placeholder="请输入内容"
              size="mini"
              clearable
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="reasonVisible = false">取 消</el-button>
        <el-button
          :loading="addLableLoading"
          :disabled="addLableLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveReason"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import naTable from "@/components/naTable";
import perRedioDialog from "@/components/perDialogRedio";
import { arraySort, filterSpecifications } from "@/utils";
import { mapGetters } from "vuex";

export default {
  name: "Purchase",
  components: {
    naTable,
    perRedioDialog,
  },
  computed: {
    ...mapGetters(["name", "userInfo"]),
  },
  data() {
    return {
      isSee: true, // 是否是查看操作
      isArrivalSee: false, // 采购到货详情查看弹框
      isArrivalConfrim: false, // 到货确认弹框
      ops: "add", // 采购操作、新建，修改，复制重开
      isSeePurchase: false, // 查看弹框是否打开
      marLoading: false, // 所有物料加载效果
      addLableLoading: false,
      isPurchaseInfo: false, // 新增明细弹框
      isperShow: false, // 选中人员弹框
      // 设置时间控件小于当前日期不能选择
      option: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 1 * 24 * 3600 * 1000;
        },
      },
      purchaseForm: {
        purchaseOrderNo: "系统自动生成", // 采购订单号
        purchaseType: "",
        supplierCode: "",
        purchasePersonCode: "",
        expectArrivalTime: "",
        purchaseDesc: "",
        purchaseDetailsList: [],
      }, // 新建采购表单
      addPurLoading: false, // 新建采购保存加载效果
      searchLoading: false, // 采购列表加载效果
      // 采购列表查询条件
      search: {
        QueryDataBaseFiled: "",
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
        inStockState: -2,
        purchaseOrderState: -2,
      },
      purchasDatas: [], // 采购列表数据
      total: 0, // 采购数据总条数
      purchaseTypes: [], // 所有采购类型列表
      tableHead: [], // 表头
      tableHeight: null, // 表格最大高度
      dialogTableHeight: null, // 弹框里面得表格最大高度
      // 选择物料搜索条件
      materSearch: {
        likeWords: "",
        supplierCode: "",
        warehouseCode: "",
        warehouseAreaCode: "",
      },
      purchaseID: "", // 采购ID
      isAddPurchase: false, // 采购新建弹框是否打开
      allSupliers: [], // 供应商列表
      materierlLists: [], // 选中物料
      allMateriels: [], // 当前供应商下面的物料
      rules: {
        purchaseType: [
          {
            required: true,
            message: "请选择采购类型",
            trigger: "change",
          },
        ],
        supplierCode: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "change",
          },
        ],
        expectArrivalTime: [
          {
            required: true,
            message: "请选择期望到货时间",
            trigger: "change",
          },
        ],
      },

      cancelForm: {
        purchaseID: 0,
        cancelReason: "",
        reason: "",
        purchaseOrderState: 0,
      },
      title: "",
      reasonVisible: false,
      cancelRules: {
        cancelReason: [
          {
            required: true,
            message: "请填写原因",
            trigger: ["change", "blur"],
          },
        ],
      },

      materialList: [],

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "purchaseID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        // addTitle: '', //新建按钮名称-为''则不显示，为undefine则为新建
        addPermission: "MenuCode5-4-1", //新建按钮权限
        addIcon: "", //新建按钮icon
        voidWord: "", //需要查询作废的字段
        statusList: [], //状态筛选条件列表
        statusWord: "", //状态筛选的字段
        refresh: true, //刷新按钮
        showSum: false, //显示合计
        //表格按钮
        operationBtn: [
          {
            title: "查看", //按钮名称
            methodsName: "seePurchase", //按钮绑定方法的字段
          },
          {
            title: "编辑", //按钮名称
            methodsName: "updatedPurchase", //按钮绑定方法的字段
            permission: "MenuCode5-4-2", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "purchaseOrderState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          // {
          //   title: "确认", //按钮名称
          //   methodsName: "confirmClick", //按钮绑定方法的字段
          //   permission: "MenuCode5-4-3", //按钮权限
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "purchaseOrderState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       "待确认",
          //     ],
          //   },
          // },
          {
            title: "作废", //按钮名称
            methodsName: "deletePurchase", //按钮绑定方法的字段
            permission: "MenuCode5-4-4", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "inStockState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                0,-1
              ],
            },
          },
          {
            title: "审核", //按钮名称
            methodsName: "arrivalClick", //按钮绑定方法的字段
            permission: "MenuCode5-4-5", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "purchaseOrderState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待审核",
              ],
            },
          },
          {
            title: "打印请购单", //按钮名称
            methodsName: "handleDownload", //按钮绑定方法的字段
          },
          // {
          //   title: "打印采购对账单", //按钮名称
          //   methodsName: "handleBillDownload", //按钮绑定方法的字段
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "inStockStateName", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       "已入库",
          //     ],
          //   },
          // },
          {
            title: "打印采购单", //按钮名称
            methodsName: "handlePurchaseDownload", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "purchaseOrderState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "已完成"
              ],
            },
          },
          // {
          //   title: "复制", //按钮名称
          //   methodsName: "copyPurchase", //按钮绑定方法的字段
          //   permission: "MenuCode5-4-6", //按钮权限
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "isCopy", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       1,
          //     ],
          //   },
          // },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },
    };
  },
  created() {
    // 采购列表
    this.getAllPurchase();
    // 采购列表表格头部
    this.getTableHead();
    // 供应商
    this.getSupplier();
    // 所有采购类型
    this.getAllPurTypes();
  },
  mounted() {},
  methods: {
    // 总金额发生改变
    totalAmountUpdate(row) {
      if (row.isPriceUnit == 0) {
        row.purchasePrice = parseFloat(
          (row.totalAmount / row.purchaseQuantity).toFixed(6)
        );
      } else {
        row.specificationPrice = parseFloat(
          (row.totalAmount / row.specificationQuantity).toFixed(6)
        );
        row.purchasePrice = parseFloat(
          (row.specificationPrice / row.specificationJsValue).toFixed(6)
        );
      }
    },

    //保存原因
    saveReason() {
      this.$refs.cancelForm.validate((valid) => {
        if (valid) {
          if (this.title == "拒绝") {
            this.cancelForm.purchaseOrderState = 3;
          } else {
            this.cancelForm.purchaseOrderState = 4;
          }
          this.saveOrderState();
        }
      });
    },

    //提交原因
    saveOrderState() {
      this.addLableLoading = true;
      this.$api.warehouse
        .VoidPurchaseOrder(this.cancelForm)
        .then((res) => {
          this.getAllPurchase();
          this.isArrivalConfrim = false;
          this.reasonVisible = false;
        })
        .catch(() => {
          this.addLableLoading = false;
        });
    },

    closeCancelDialog() {
      this.cancelForm.cancelReason = "";
    },

    getDate() {
      return this.dayjs().format("YYYY-MM-DD");
    },

    // 换算单价改变
    specificationPriceUpdate(row) {
      if (row.isPriceUnit != 0) {
        row.totalAmount = parseFloat(
          (row.specificationQuantity * row.specificationPrice).toFixed(6)
        );
        row.purchasePrice = parseFloat(
          (row.specificationPrice / row.specificationJsValue).toFixed(6)
        );
      }
    },
    // 单价改变
    purchasePriceUpdate(row) {
      if (row.isPriceUnit == 0) {
        row.totalAmount = parseFloat(
          (row.purchaseQuantity * row.purchasePrice).toFixed(6)
        );
      } else {
        row.specificationPrice = parseFloat(
          (row.purchasePrice * row.specificationJsValue).toFixed(6)
        );
        row.totalAmount = parseFloat(
          (row.specificationPrice * row.specificationQuantity).toFixed(6)
        );
      }
    },
    // 到货数量更改
    quantityUpdate(row) {
      if (row.isPriceUnit == 0) {
        row.totalAmount = parseFloat(
          (row.purchaseQuantity * row.purchasePrice).toFixed(6)
        );
      } else {
        row.specificationQuantity = parseFloat(
          (row.purchaseQuantity * row.specificationJsValue).toFixed(6)
        );
        row.totalAmount = parseFloat(
          (row.specificationQuantity * row.specificationPrice).toFixed(6)
        );
      }
    },

    //下载请购单
    handleDownload(row) {
      let parmas = {
        templateTypeCode: "MBLX0010", //	模板类型编号
        customerCode: row.customerCode,
        condition: {
          PurchaseOrderNo: row.purchaseOrderNo,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    //下载采购单
    handlePurchaseDownload(row){
      let parmas = {
        templateTypeCode: "MBLX0016", //	模板类型编号
        customerCode: row.customerCode,
        condition: {
          PurchaseOrderNo: row.purchaseOrderNo,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    //下载采购对账单
    handleBillDownload(row) {
      let parmas = {
        templateTypeCode: "MBLX0012", //	模板类型编号
        customerCode: row.customerCode,
        condition: {
          PurchaseOrderNo: row.purchaseOrderNo,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    //拒绝
    handleRefuse() {
      this.title = "拒绝";
      this.reasonVisible = true;
    },

    // 通过
    arrivalConfirm() {
      const params = this.purchaseForm;
      this.$api.warehouse.ConfirmGoodsPurchaseOrder(params).then(() => {
        this.$message({
          message: "通过成功",
          type: "success",
        });
        this.getAllPurchase();
        this.isArrivalConfrim = false;
      });
    },
    // 供应商选择改变事件
    supplierChange(val) {
      // this.materSearch.supplierCode = val;
      this.purchaseForm.purchaseDetailsList = [];
      this.materialList = [];
      this.purchaseForm.purchaseDetailsList.push({
        goodsCode: "",
        specificationCode: "",
        planPurchaseQuantity: 0,
        purchaseDesc: "",
        goodsPrice: 0,
        dateOfManufacture: "",
      });
      this.$api.mater
        .GetGoodsSpecificationBySupplierCode({
          queryBy: 0,
          queryValue: val,
          OnlyPriceAdjustmented:true
        })
        .then((res) => {
          this.materialList = res;
        });
    },

    //删除
    handleDeleteRow(row, index) {
      this.purchaseForm.purchaseDetailsList.splice(index, 1);
    },

    //复制
    handleCopyRow(row, index) {
      let copyRow = JSON.parse(JSON.stringify(row));
      copyRow.rowKey = Date.now();
      this.purchaseForm.purchaseDetailsList.splice(
        index,
        0,
        JSON.parse(JSON.stringify(copyRow))
      );
    },

    //物料下拉选择事件
    handleMaterialNameChange(row, index, e) {
      if (index == this.purchaseForm.purchaseDetailsList.length - 1) {
        this.purchaseForm.purchaseDetailsList.push({
          supplierCode: "",
          stockGoodsCode: "",
          planPurchaseQuantity: 0,
          purchaseDesc: "",
          goodsSpecificationList: [],
          rowKey: Date.now(),
        });
      }
      // this.materialList[index].stockGoodsCode = e;
      row.specificationCode = "";
      this.materialList.forEach((item, i) => {
        if (item.goodsCode == e) {
          row.materialType = item.categoryName;
          row.materialName = item.goodsName;
          this.$set(row, "goodsSpecificationList", item.goodsSpecificationList);
          // row.goodsSpecificationList = item.goodsSpecificationList;
          row.goodsUnit = item.goodsUnit;
          row.goodsType = item.goodsType;
          row.priceUnit = item.priceUnit;
        }
      });
      // row.productMaterialCode = e.goodsCode;
      // row.materialType = e.categoryName;
      // row.materialName = e.goodsName;
      // this.getStockGoodsList(e);
    },

    //规格型号选择事件
    handleSpecificationChange(row, index) {
      this.purchaseForm.purchaseDetailsList.forEach((item) => {
        if (
          item.goodsSpecificationList &&
          item.goodsSpecificationList.length > 0
        ) {
          item.goodsSpecificationList.forEach((x) => {
            if (item.specificationCode == x.specificationCode) {
              item.stockQuantity = x.stockQuantity;
              item.goodsPrice = x.goodsPrice;
              item.specificationJsValue = x.specificationJsValue;
              item.specificationCode = x.specificationCode;
            }
          });
        }
      });
      this.$forceUpdate();
    },

    // 新建采购新增采购明细事件
    newAddClick() {
      if (this.purchaseForm.supplierCode === "") {
        this.$message({
          message: "请选选择供应商",
          type: "warning",
        });
        return;
      }
      // 所有物料
      this.getAllMarList();
      this.isPurchaseInfo = true;
    },
    // 打开新建采购弹框
    addPurchaseDialog() {
      this.isAddPurchase = true;
      this.materialList = [];
      this.ops = "add";
      this.purchaseForm.purchasePersonCode = this.userInfo.employeeCode;
      this.purchaseForm.purchasePersonName = this.userInfo.employeeName;
      this.purchaseForm.purchaseDetailsList.push({
        supplierCode: "",
        stockGoodsCode: "",
        planPurchaseQuantity: 0,
        purchaseDesc: "",
      });
    },
    // 所有物料
    getAllMarList() {
      const parmas = this.materSearch;
      this.$api.mater.GetMatchStockGoodss(parmas).then((res) => {
        this.allMateriels = res;
      });
    },
    // 确认采购
    confirmPuchase() {
      const parmas = {
        entityID: this.purchaseID,
      };
      this.$confirm("确定采购?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.addPurLoading = true;
        this.$api.warehouse
          .confirmPuchase(parmas)
          .then(() => {
            this.isSeePurchase = false;
            this.addPurLoading = false;
            this.getAllPurchase();
            this.$message({
              message: "确认成功",
              type: "success",
            });
          })
          .catch(() => {
            this.addPurLoading = false;
          });
      });
    },
    // 查看采购详情
    GetPurInfo(id) {
      const parmas = {
        queryBy: 1,
        queryValue: id,
      };
      this.$api.warehouse.getPurINfo(parmas).then((res) => {
        this.$api.mater
          .GetGoodsSpecificationBySupplierCode({
            queryBy: 0,
            queryValue: res.supplierCode,
            OnlyPriceAdjustmented:true
          })
          .then((data) => {
            res.purchaseDetailsList.forEach((m, i) => {
              m.goodsType = m.categoryName;
              m.goodsPrice = m.purchasePrice;
              m.rowKey = i;
              m.stockQuantity = m.categoryName;
              data.forEach((x) => {
                if (m.goodsCode == x.goodsCode) {
                  m.goodsUnit = x.goodsUnit;
                  m.goodsType = x.goodsType;
                  m.priceUnit = x.priceUnit;
                  this.$set(
                    m,
                    "goodsSpecificationList",
                    filterSpecifications(
                      x.goodsSpecificationList,
                      m.specificationCode
                    )
                  );
                  // m.goodsSpecificationList = x.goodsSpecificationList;
                  if (
                    m.goodsSpecificationList &&
                    m.goodsSpecificationList.length > 0
                  ) {
                    m.goodsSpecificationList.forEach((k) => {
                      if (m.specificationCode == k.specificationCode) {
                        m.stockQuantity = k.stockQuantity;

                        m.specificationCode = k.specificationCode;
                        m.specificationJsValue = k.specificationJsValue;
                        const price = Number(m.purchasePrice) || 0;
                        const num = Number(m.planPurchaseQuantity) || 0;
                        m.subtotal =
                          num * price * Number(k.specificationJsValue);
                        m.subtotal = JSON.parse(m.subtotal.toFixed(4));
                      }
                    });
                  }
                }
              });
            });
            this.materialList = data;
            this.purchaseForm = res;
            if (this.isSee) {
              this.purchaseForm.purchaseDetailsList.push({
                goodsCode: "",
                specificationCode: "",
                planPurchaseQuantity: 0,
                purchaseDesc: "",
                goodsPrice: 0,
                dateOfManufacture: "",
              });
            }
          });
      });
    },
    // 查看采购点击事件
    seePurchase(data) {
      this.ops = "modify";

      if (data.purchaseOrderState !== 2) {
        this.isSee = false;
        this.GetPurInfo(data.purchaseID);
        this.isSeePurchase = true;
      } else {
        this.GetPurInfo(data.purchaseID);
        this.isArrivalSee = true;
      }
    },
    // 到货确认
    arrivalClick(data) {
      this.cancelForm.purchaseID = data.purchaseID;
      this.isSee = false;
      this.GetPurInfo(data.purchaseID);
      this.isArrivalConfrim = true;
    },
    // 修改采购点击事件
    updatedPurchase(data) {
      this.ops = "modify";
      this.GetPurInfo(data.purchaseID);
      this.purchaseID = data.purchaseID
      this.isSee = true;
      this.isAddPurchase = true;
    },
    // 采购确认点击事件
    confirmClick(data) {
      // 采购确认
      this.purchaseID = data.purchaseID
      this.isSee = false;
      this.ops = "confirm";
      this.GetPurInfo(data.purchaseID);
      this.isSeePurchase = true;
    },
    // 采购复制点击事件
    copyPurchase(data) {
      this.ops = "copy";
      this.GetPurInfo(data.purchaseID);
      this.isAddPurchase = true;
    },
    // 作废
    deletePurchase(row) {
      this.title = "作废";
      this.cancelForm.purchaseID = row.purchaseID;
      this.reasonVisible = true;
    },
    // 修改保存
    editSavePuchase(type) {
      if (this.purchaseForm.purchaseDetailsList.length > 0) {
        this.purchaseForm.purchaseDetailsList.pop();
      }
      const parmas = this.purchaseForm;
      this.$refs.purchaseForm.validate((valid) => {
        if (valid) {
          this.addPurLoading = true;
          if (type === "confirm") {
            this.$api.warehouse
              .UpdatePurchaseOrder(parmas)
              .then(() => {
                const parmas = {
                  entityID: this.purchaseForm.purchaseID,
                };
                this.addPurLoading = true;
                this.$api.warehouse
                  .confirmPuchase(parmas)
                  .then(() => {
                    this.addPurLoading = false;
                    this.isAddPurchase = false;
                    this.getAllPurchase();
                    this.$message({
                      message: "确认成功",
                      type: "success",
                    });
                  })
                  .catch(() => {
                    this.addPurLoading = false;
                  });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          } else {
            this.$api.warehouse
              .UpdatePurchaseOrder(parmas)
              .then(() => {
                this.addPurLoading = false;
                this.isAddPurchase = false;
                this.getAllPurchase();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          }
        }
      });
    },
    // 复制重开保存
    copyReopen() {
      const parmas = this.purchaseForm;
      this.$refs.purchaseForm.validate((valid) => {
        if (valid) {
          this.addPurLoading = true;
          this.$api.warehouse
            .AddCBPurchaseOrder(parmas)
            .then(() => {
              this.addPurLoading = false;
              this.isAddPurchase = false;
              this.getAllPurchase();
              this.$message({
                message: "保存成功",
                type: "success",
              });
            })
            .catch(() => {
              this.addPurLoading = false;
            });
        }
      });
    },

    // 新建采购保存
    savePuchase(type) {
      this.$refs.purchaseForm.validate((valid) => {
        const parmas = this.purchaseForm;
        if (valid) {
          if (this.purchaseForm.purchaseDetailsList.length > 1) {
            this.purchaseForm.purchaseDetailsList.pop();
          }
          this.addPurLoading = true;
          if (type === "confirm") {
            this.$api.warehouse
              .AddPurchaseOrder(parmas)
              .then((res) => {
                const parmas = {
                  entityID: res.purchaseID,
                };
                this.addPurLoading = true;
                this.$api.warehouse
                  .confirmPuchase(parmas)
                  .then(() => {
                    this.addPurLoading = false;
                this.isAddPurchase = false;
                this.getAllPurchase();
                    // this.$message({
                    //   message: "确认成功",
                    //   type: "success",
                    // });
                  
                  })
                  .catch(() => {
                    this.addPurLoading = false;
                  });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          } else {
            this.$api.warehouse
              .AddPurchaseOrder(parmas)
              .then(() => {
                this.addPurLoading = false;
                this.isAddPurchase = false;
                this.getAllPurchase();
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          }
        }
      });
    },
    // 技术采购总价
    getPurTotalPrice(data) {
      const price = Number(data.goodsPrice) || 0;
      const num = Number(data.planPurchaseQuantity) || 0;
      data.subtotal = num * price * Number(data.specificationJsValue);
      data.subtotal = JSON.parse(data.subtotal.toFixed(4));
      this.$forceUpdate();
    },

    // 选择物料
    confirmSelecd() {
      const array = JSON.parse(
        JSON.stringify(this.purchaseForm.purchaseDetailsList)
      );
      const arr = JSON.parse(JSON.stringify(this.materierlLists));
      arr.forEach((item) => {
        item.planPurchaseQuantity = 0;
        item.purchaseDesc = "";
      });
      this.purchaseForm.purchaseDetailsList = arr.reduce((result, item) => {
        const val = result.some(
          ({ stockGoodsCode }) => stockGoodsCode === item.stockGoodsCode
        );
        if (!val) {
          result.push(item);
        }
        return result;
      }, array);
      this.isPurchaseInfo = false;
    },
    // 商品明细表格选中复选框操作
    on_SelectSion(val) {
      this.materierlLists = val;
    },
    // 得到申请人员
    getEmployee(val) {
      this.purchaseForm.purchasePersonCode = val.employeeCode;
      this.purchaseForm.purchasePersonName = val.employeeName;
      this.purchaseForm.organizationCode = val.organizationCode;
      this.purchaseForm.organizationName = val.organizationName;
      this.purchaseForm.departmentCode = val.departmentCode;
      this.purchaseForm.departmentName = val.departmentName;
    },
    // 所有采购类型
    getAllPurTypes() {
      this.$api.warehouse.GetAllPurchaseType().then((res) => {
        this.purchaseTypes = res;
      });
    },
    // 供应商
    getSupplier() {
      this.$api.mater.GetAllSuppliers().then((res) => {
        this.allSupliers = res;
      });
    },
    // 合计方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          column.property === "totalCount" ||
          column.property === "totalAmount"
        ) {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index]++;
          } else {
            sums[index] = " ";
          }
        }
      });

      return sums;
    },
    // 采购列表
    getAllPurchase() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.warehouse
        .getAllPurchaseList(parmas)
        .then((res) => {
          this.searchLoading = false;
          if (res.list.length > 0) {
            res.list.forEach((item) => {
              if (item.purchaseOrderState == 0) {
                item.purchaseOrderState = "待确认";
              } else if (item.purchaseOrderState == 1) {
                item.purchaseOrderState = "待审核";
              } else if (item.purchaseOrderState == 2) {
                item.purchaseOrderState = "已完成";
              } else if (item.purchaseOrderState == 3) {
                item.purchaseOrderState = "已拒绝";
              } else if (item.purchaseOrderState == 4) {
                item.purchaseOrderState = "已作废";
              }
            });
          }
          this.tableData.tbody = res.list;
          this.search.total = res.totalCount;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
    // 采购列表表格头部
    getTableHead() {
      const parmas = {
        tableCode: this.$config.purchaseOrderList,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
      });
    },
    // 关闭弹框事件
    closeDialog() {
      // 关闭弹框的时候清空数据
      if (this.$refs.purchaseForm) {
        this.$refs.purchaseForm.resetFields();
      }
      this.purchaseForm = this.$options.data().purchaseForm;
    },
  },
};
</script>
<style lang="scss" scoped>
.spot {
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 100%;
}

.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
</style>
